package alipay

import (
	"jlpay-sdk-go/pkg/jlpay/core"
	"jlpay-sdk-go/pkg/jlpay/models"
)

// ==================== 支付宝WAP支付请求 ====================

// WapPayRequest 支付宝WAP支付请求
type WapPayRequest struct {
	models.OrgBaseRequest
	// MchID 商户号
	MchID string `json:"mch_id,omitempty"`
	// TermNo 终端号
	TermNo string `json:"term_no,omitempty"`
	// PayType 交易类型
	PayType string `json:"pay_type,omitempty"`
	// BuyerLogonID 买家支付宝账号
	BuyerLogonID string `json:"buyer_logon_id,omitempty"`
	// BuyerID 买家支付宝用户ID
	BuyerID string `json:"buyer_id,omitempty"`
	// LimitPay 指定支付方式
	LimitPay string `json:"limit_pay,omitempty"`
	// OutTradeNo 外部订单号
	OutTradeNo string `json:"out_trade_no,omitempty"`
	// PaymentValidTime 订单有效期
	PaymentValidTime string `json:"payment_valid_time,omitempty"`
	// Body 商品标题
	Body string `json:"body,omitempty"`
	// Attach 商品描述
	Attach string `json:"attach,omitempty"`
	// TotalFee 交易金额
	TotalFee string `json:"total_fee,omitempty"`
	// IsHirePurchase 分期标识 0：否 1：是
	IsHirePurchase string `json:"is_hire_purchase,omitempty"`
	// HirePurchaseNum 分期数
	HirePurchaseNum string `json:"hire_purchase_num,omitempty"`
	// HirePurchaseSellerPercent 卖家承担的手续费比例
	HirePurchaseSellerPercent string `json:"hire_purchase_seller_percent,omitempty"`
	// Remark 备注
	Remark string `json:"remark,omitempty"`
	// NotifyURL 回调地址
	NotifyURL string `json:"notify_url,omitempty"`
	// OpUserID 操作员
	OpUserID string `json:"op_user_id,omitempty"`
	// OpShopID 门店号
	OpShopID string `json:"op_shop_id,omitempty"`
	// DeviceInfo 终端设备号
	DeviceInfo string `json:"device_info,omitempty"`
	// MchCreateIP 终端IP
	MchCreateIP string `json:"mch_create_ip,omitempty"`
	// Longitude 经度
	Longitude string `json:"longitude,omitempty"`
	// Latitude 纬度
	Latitude string `json:"latitude,omitempty"`
	// TerminalInfo 终端信息
	TerminalInfo interface{} `json:"terminal_info,omitempty"`
	// 原始请求映射数据
	rawRequestMap map[string]interface{} `json:"-"`
}

// NewWapPayRequest 创建支付宝WAP支付请求
func NewWapPayRequest() *WapPayRequest {
	return &WapPayRequest{}
}

// SetMchID 设置商户号
func (r *WapPayRequest) SetMchID(mchID string) *WapPayRequest {
	r.MchID = mchID
	return r
}

// SetTermNo 设置终端号
func (r *WapPayRequest) SetTermNo(termNo string) *WapPayRequest {
	r.TermNo = termNo
	return r
}

// SetPayType 设置交易类型
func (r *WapPayRequest) SetPayType(payType string) *WapPayRequest {
	r.PayType = payType
	return r
}

// SetBuyerLogonID 设置买家支付宝账号
func (r *WapPayRequest) SetBuyerLogonID(buyerLogonID string) *WapPayRequest {
	r.BuyerLogonID = buyerLogonID
	return r
}

// SetBuyerID 设置买家支付宝用户ID
func (r *WapPayRequest) SetBuyerID(buyerID string) *WapPayRequest {
	r.BuyerID = buyerID
	return r
}

// SetLimitPay 设置指定支付方式
func (r *WapPayRequest) SetLimitPay(limitPay string) *WapPayRequest {
	r.LimitPay = limitPay
	return r
}

// SetOutTradeNo 设置外部订单号
func (r *WapPayRequest) SetOutTradeNo(outTradeNo string) *WapPayRequest {
	r.OutTradeNo = outTradeNo
	return r
}

// SetPaymentValidTime 设置订单有效期
func (r *WapPayRequest) SetPaymentValidTime(paymentValidTime string) *WapPayRequest {
	r.PaymentValidTime = paymentValidTime
	return r
}

// SetBody 设置商品标题
func (r *WapPayRequest) SetBody(body string) *WapPayRequest {
	r.Body = body
	return r
}

// SetAttach 设置商品描述
func (r *WapPayRequest) SetAttach(attach string) *WapPayRequest {
	r.Attach = attach
	return r
}

// SetTotalFee 设置交易金额
func (r *WapPayRequest) SetTotalFee(totalFee string) *WapPayRequest {
	r.TotalFee = totalFee
	return r
}

// SetIsHirePurchase 设置分期标识
func (r *WapPayRequest) SetIsHirePurchase(isHirePurchase string) *WapPayRequest {
	r.IsHirePurchase = isHirePurchase
	return r
}

// SetHirePurchaseNum 设置分期数
func (r *WapPayRequest) SetHirePurchaseNum(hirePurchaseNum string) *WapPayRequest {
	r.HirePurchaseNum = hirePurchaseNum
	return r
}

// SetHirePurchaseSellerPercent 设置卖家承担的手续费比例
func (r *WapPayRequest) SetHirePurchaseSellerPercent(hirePurchaseSellerPercent string) *WapPayRequest {
	r.HirePurchaseSellerPercent = hirePurchaseSellerPercent
	return r
}

// SetRemark 设置备注
func (r *WapPayRequest) SetRemark(remark string) *WapPayRequest {
	r.Remark = remark
	return r
}

// SetNotifyURL 设置回调地址
func (r *WapPayRequest) SetNotifyURL(notifyURL string) *WapPayRequest {
	r.NotifyURL = notifyURL
	return r
}

// SetOpUserID 设置操作员
func (r *WapPayRequest) SetOpUserID(opUserID string) *WapPayRequest {
	r.OpUserID = opUserID
	return r
}

// SetOpShopID 设置门店号
func (r *WapPayRequest) SetOpShopID(opShopID string) *WapPayRequest {
	r.OpShopID = opShopID
	return r
}

// SetDeviceInfo 设置终端设备号
func (r *WapPayRequest) SetDeviceInfo(deviceInfo string) *WapPayRequest {
	r.DeviceInfo = deviceInfo
	return r
}

// SetMchCreateIP 设置终端IP
func (r *WapPayRequest) SetMchCreateIP(mchCreateIP string) *WapPayRequest {
	r.MchCreateIP = mchCreateIP
	return r
}

// SetLongitude 设置经度
func (r *WapPayRequest) SetLongitude(longitude string) *WapPayRequest {
	r.Longitude = longitude
	return r
}

// SetLatitude 设置纬度
func (r *WapPayRequest) SetLatitude(latitude string) *WapPayRequest {
	r.Latitude = latitude
	return r
}

// SetTerminalInfo 设置终端信息
func (r *WapPayRequest) SetTerminalInfo(terminalInfo interface{}) *WapPayRequest {
	r.TerminalInfo = terminalInfo
	return r
}

// SetRawRequestMap 设置原始请求映射数据
func (r *WapPayRequest) SetRawRequestMap(rawMap map[string]interface{}) *WapPayRequest {
	r.rawRequestMap = rawMap
	return r
}

// GetRawMap 获取原始映射数据
func (r *WapPayRequest) GetRawMap() (map[string]interface{}, bool) {
	return r.rawRequestMap, r.rawRequestMap != nil
}

// ToMap 转换为map
func (r *WapPayRequest) ToMap() (map[string]interface{}, error) {
	// 如果有原始映射数据，直接返回
	if rawMap, exists := r.GetRawMap(); exists {
		return rawMap, nil
	}

	// 构建基础请求数据
	requestMap := make(map[string]interface{})

	// 添加基础字段
	if r.MchID != "" {
		requestMap["mch_id"] = r.MchID
	}
	if r.TermNo != "" {
		requestMap["term_no"] = r.TermNo
	}
	if r.PayType != "" {
		requestMap["pay_type"] = r.PayType
	}
	if r.BuyerLogonID != "" {
		requestMap["buyer_logon_id"] = r.BuyerLogonID
	}
	if r.BuyerID != "" {
		requestMap["buyer_id"] = r.BuyerID
	}
	if r.LimitPay != "" {
		requestMap["limit_pay"] = r.LimitPay
	}
	if r.OutTradeNo != "" {
		requestMap["out_trade_no"] = r.OutTradeNo
	}
	if r.PaymentValidTime != "" {
		requestMap["payment_valid_time"] = r.PaymentValidTime
	}
	if r.Body != "" {
		requestMap["body"] = r.Body
	}
	if r.Attach != "" {
		requestMap["attach"] = r.Attach
	}
	if r.TotalFee != "" {
		requestMap["total_fee"] = r.TotalFee
	}
	if r.IsHirePurchase != "" {
		requestMap["is_hire_purchase"] = r.IsHirePurchase
	}
	if r.HirePurchaseNum != "" {
		requestMap["hire_purchase_num"] = r.HirePurchaseNum
	}
	if r.HirePurchaseSellerPercent != "" {
		requestMap["hire_purchase_seller_percent"] = r.HirePurchaseSellerPercent
	}
	if r.Remark != "" {
		requestMap["remark"] = r.Remark
	}
	if r.NotifyURL != "" {
		requestMap["notify_url"] = r.NotifyURL
	}
	if r.OpUserID != "" {
		requestMap["op_user_id"] = r.OpUserID
	}
	if r.OpShopID != "" {
		requestMap["op_shop_id"] = r.OpShopID
	}
	if r.DeviceInfo != "" {
		requestMap["device_info"] = r.DeviceInfo
	}
	if r.MchCreateIP != "" {
		requestMap["mch_create_ip"] = r.MchCreateIP
	}
	if r.Longitude != "" {
		requestMap["longitude"] = r.Longitude
	}
	if r.Latitude != "" {
		requestMap["latitude"] = r.Latitude
	}
	if r.TerminalInfo != nil {
		requestMap["terminal_info"] = r.TerminalInfo
	}

	// 合并基础请求数据
	baseMap, err := r.OrgBaseRequest.ToMap()
	if err != nil {
		return nil, err
	}

	for k, v := range baseMap {
		requestMap[k] = v
	}

	return requestMap, nil
}

// CheckRequiredParams 检查必需参数
func (r *WapPayRequest) CheckRequiredParams(config *core.Config) error {
	return r.OrgBaseRequest.CheckRequiredParams(config)
}

// ==================== 支付宝WAP支付响应 ====================

// WapPayResponse 支付宝WAP支付响应
type WapPayResponse struct {
	models.StandardResponse
	// Status 状态 1：待确认 3：失败
	Status string `json:"status,omitempty"`
	// MchID 商户号
	MchID string `json:"mch_id,omitempty"`
	// TermNo 终端号
	TermNo string `json:"term_no,omitempty"`
	// DeviceInfo 终端设备号
	DeviceInfo string `json:"device_info,omitempty"`
	// TransactionID 平台订单号
	TransactionID string `json:"transaction_id,omitempty"`
	// OutTradeNo 外部订单号
	OutTradeNo string `json:"out_trade_no,omitempty"`
	// ChnTransactionID 渠道订单号
	ChnTransactionID string `json:"chn_transaction_id,omitempty"`
	// TotalFee 交易金额
	TotalFee interface{} `json:"total_fee,omitempty"`
	// PayInfo 支付信息
	PayInfo interface{} `json:"pay_info,omitempty"`
	// OrderTime 订单时间
	OrderTime string `json:"order_time,omitempty"`
	// PayType 交易类型
	PayType string `json:"pay_type,omitempty"`
	// TransTime 交易时间
	TransTime string `json:"trans_time,omitempty"`
}

// NewWapPayResponse 创建支付宝WAP支付响应
func NewWapPayResponse() *WapPayResponse {
	return &WapPayResponse{}
}
